﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;

namespace TrackerRealm.Jazz.Common
{
    /// <summary>
    /// 
    /// </summary>
    public static class IEnumberableExtensionMethods
    {
        //
        // IEnumerable<T> Extension methods
        //
        #region ForEach
        /// <summary>
        /// Foreach equivalent
        /// </summary>
        /// <param name="members"></param>
        /// <returns></returns>
        public static void Foreach<T>(this IEnumerable<T> enumeration, Action<T> action)
        {
            foreach (T item in enumeration)
            {
                action(item);
            }
        }
        #endregion
        #region Add
        /// <summary>
        /// Adds 1 element to enumeration.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumeration"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static IEnumerable<T> AddToSequence<T>(this IEnumerable<T> enumeration, T element)
        {
            foreach (T item in enumeration)
            {
                yield return item;
            }
            yield return element;
        }
        /// <summary>
        /// Adds 1 element to enumeration.
        /// <para>Throws an 'ArgumentException' if the enumeration already contains the element.</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumeration"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static IEnumerable<T> AddToUniqueSequence<T>(this IEnumerable<T> enumeration, T element)
        {
            if (enumeration.Contains(element))
                throw new ArgumentException("The element is already a member of the sequence.", "element");
            foreach (T item in enumeration)
            {
                yield return item;
            }
            yield return element;
        }
        /// <summary>
        /// Adds 1 or more elements to enumeration.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumeration"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static IEnumerable<T> AddToSequence<T>(this IEnumerable<T> enumeration, params T[] element)
        {
            foreach (T item in enumeration)
            {
                yield return item;
            }
            foreach (T item in element)
            {
                yield return item;
            }
        }
        #endregion
        #region Remove
        /// <summary>
        /// Removes 1 or more elements from the enumeration.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumeration"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static IEnumerable<T> RemoveFromSequence<T>(this IEnumerable<T> enumeration, params T[] element)
        {
            return enumeration.Except(element);
        }
        #endregion
    }
}
